package com.yq.controller;

import com.netflix.discovery.converters.Auto;
import com.yq.service.EmpService;
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.vavr.control.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/dept")
public class EmpController {

    @Autowired
    EmpService empService;

    @Autowired
    CircuitBreaker circuitBreaker;

    @GetMapping("/info")
    public String info(){
        return (String) Try.ofSupplier(
                CircuitBreaker.decorateSupplier(circuitBreaker,
                        () -> empService.getById(100)))
                .recover(RuntimeException.class, "服务已关闭")
                .get();
//        return empService.getById(100);
    }

    @GetMapping("/base")
    public String base(){
        return empService.list();
    }

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/foo")
    public String foo(){
        return (String) Try.ofSupplier(
                CircuitBreaker.decorateSupplier(circuitBreaker,
                        () -> restTemplate.getForObject("http://EMP/emp/30",String.class)))
                .recover(RuntimeException.class, "服务已关闭")
                .get();
//        return restTemplate.getForObject("http://EMP/emp/30",String.class);
    }

    @GetMapping("/bar")
    public String bar(){
        return restTemplate.getForObject("http://EMP/emp/list",String.class);
    }

    @Value("${age}")
    String age;
    @GetMapping("/data")
    public String data(){
        return age;
    }
}
